package com.health.push.service;

import com.health.push.entity.User;
import com.health.push.dto.LoginRequest;
import com.health.push.dto.RegisterRequest;
import org.springframework.stereotype.Service;

import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;

@Service
public class UserService {

    // 使用内存存储用户信息
    private final Map<String, User> users = new ConcurrentHashMap<>();

    public boolean register(RegisterRequest request) {
        if (users.containsKey(request.getUsername())) {
            return false; // 用户已存在
        }

        User user = new User();
        user.setUsername(request.getUsername());
        user.setPassword(request.getPassword()); // 实际项目中应该加密密码
        user.setEmail(request.getEmail());
        user.setCreateTime(System.currentTimeMillis());

        users.put(request.getUsername(), user);
        return true;
    }

    public User login(LoginRequest request) {
        User user = users.get(request.getUsername());
        if (user != null && user.getPassword().equals(request.getPassword())) {
            return user;
        }
        return null;
    }

    public String checkBind(String account) {
        if (account != null) {
            return "user";
        }
        return null;
    }

    public User findByUsername(String username) {
        return users.get(username);
    }

    public boolean userExists(String username) {
        return users.containsKey(username);
    }
}